import { BadRequestException, Controller, Get, Inject, Query, Redirect } from '@nestjs/common';
import { ShortUrlService } from './short-url.service';

@Controller('short-url')
export class ShortUrlController {
    constructor(
        private shortUrlService : ShortUrlService
    ){}

    @Get()
    generate(@Query("url") longUrl:string){
        return this.shortUrlService.generate(longUrl)
    }

    @Get('get-long')
    @Redirect()
    async getLong(@Query("code") code:string ){
        let url = await this.shortUrlService.getLongUrl(code) 
        if(!url){
            throw new BadRequestException('短链不存在');
        }
        return {
            statusCosde:302,
            url
        }
    }
}
